package com.itfenghuang.controller;

import com.itfenghuang.bean.Student;
import com.itfenghuang.service.StuService;
import com.itfenghuang.service.imp.StuServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 表示层代码  目前写servlet
 */
@SuppressWarnings("all")
public class StuServlet extends HttpServlet {
    private StuService service=new StuServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String sname = req.getParameter("sname");
        String sage = req.getParameter("sage");
        String ssex = req.getParameter("ssex");
        Student stu = new Student();
        stu.setSname(sname);
        if (!"".equals(sage)){
            stu.setSage(Integer.parseInt(sage));
        }
        stu.setSsex(ssex);
        List<Student> stus = service.selectStuAndClaByCondition(stu);
        if (stus!=null && !stus.isEmpty()){
            req.setAttribute("stus",stus);
            req.getRequestDispatcher("/selectStu.jsp").forward(req,resp);
        }else {
            //定时刷新
            resp.setHeader("Refresh","3;URL=/stusys/selectStu.jsp");
            resp.getWriter().write("没有数据,三秒后跳转到页面");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    }
}
